package cn.thens.kdroid.core.storage

import cn.thens.kdroid.core.app.Rx
import io.reactivex.Single

class RxStorage<K, V>(private val storage: Storage<K, V>) {
    fun node(key: K): Node<V> {
        return Node(storage.node(key))
    }

    fun clear(): Single<Unit> {
        return Rx.single { storage.clear() }
    }

    fun getAll(): Single<Map<K, V>> {
        return Rx.single { storage.getAll() }
    }

    fun getKeys(): Single<Set<K>> {
        return Rx.single { storage.getKeys() }
    }

    class Node<V>(private val node: Storage.Node<V>) {
        fun get(): Single<V> {
            return Rx.single { node.get() }
        }

        fun put(value: V): Single<Unit> {
            return Rx.single { node.put(value) }
        }

        fun clear(): Single<Unit> {
            return Rx.single { node.clear() }
        }

        fun exists(): Single<Boolean> {
            return Rx.single { node.exists() }
        }
    }
}
